package com.sina.shop.service.business;

import com.sina.shop.core.exception.client.DataException;
import com.sina.shop.core.exception.server.CreateException;
import com.sina.shop.core.exception.server.ResourceExistsException;
import com.sina.shop.core.exception.server.UpdateException;
import com.sina.shop.dao.business.BusinessDao;
import com.sina.shop.domain.business.Business;
import com.sina.shop.service.ShopDataSourceService;
import com.sina.shop.util.Assert;
import com.sina.shop.util.ResultCode;
import com.sina.shop.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.sina.shop.service.ServiceConstant.SHOP_TX;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2018-02-06-13:58
 */
@Service("businessService")
public class BusinessServiceImpl extends ShopDataSourceService implements BusinessService {

    @Autowired
    @Qualifier("businessDao")
    private BusinessDao businessDao;

    @Override
    @Transactional(value = SHOP_TX, readOnly = true, isolation = Isolation.READ_COMMITTED)
    public List<Business> getBusinessByAdminId(Long adminId) {
        return businessDao.getBusinessByAdminId(adminId);
    }

    @Override
    @Transactional(value = SHOP_TX, readOnly = true, isolation = Isolation.READ_COMMITTED)
    public List<Business> getBusinessList(Business business, Long count[]) {

        if (count != null && count.length > 0) {
            count[0] = businessDao.getBusinessCount(business);
        }

        return businessDao.getBusinessList(business);
    }


    @Override
    @Transactional(value = SHOP_TX, isolation = Isolation.REPEATABLE_READ)
    public Integer deleteBusinessTab(Long id) throws DataException, UpdateException {
        Business business = businessDao.get(Business.class, id);
        Assert.assertNotNull(business, "Business[id=%s] not exists", business.getId());

        try {
            return businessDao.deleteBusinessTab(id);
        } catch (DataAccessException e) {
            LOG.error(" {} deleteTab Business error.", Business.class, e);
            throw new UpdateException(ResultCode.dataAccessError, e.getMessage(), e);
        }
    }

    @Override
    @Transactional(value = SHOP_TX, isolation = Isolation.READ_COMMITTED)
    public Business saveBusiness(Business business) throws CreateException, ResourceExistsException, DataException {
        boolean exists;
        exists = businessDao.isExistsByUnique(Business.class, "name", business.getName());
        Assert.assertFalse(exists, ResourceExistsException.class, ResultCode.dataExists
                , "Business[%s] exists", business.getName());

        try {
            businessDao.save(business);
            businessDao.flush();
            return business;
        } catch (DataAccessException e) {
            LOG.error("{} save Business error.", Business.class, e);
            throw new UpdateException(ResultCode.dataAccessError, e.getMessage(), e);
        }
    }

    @Override
    @Transactional(value = SHOP_TX, isolation = Isolation.REPEATABLE_READ)
    public Business updateBusiness(Business business) throws UpdateException, DataException {
        Business b = businessDao.get(Business.class, business.getId());
        Assert.assertNotNull(b, "Business[id=%s] not exists", business.getId());

        try {
            b = updateBusinessFiled(business, b);
            businessDao.flush();

            return b;
        } catch (DataAccessException e) {
            LOG.error("{} update Business error.", Business.class, e);
            throw new UpdateException(ResultCode.dataAccessError, e.getMessage(), e);
        }
    }

    private Business updateBusinessFiled(Business oldBusiness, Business newBusiness) throws CreateException {

        if (!StringUtils.isEmpty(oldBusiness.getName())) {
            newBusiness.setName(oldBusiness.getName());
        }

        if (!StringUtils.isEmpty(oldBusiness.getStatus())) {
            newBusiness.setStatus(oldBusiness.getStatus());
        }

        if (!StringUtils.isEmpty(oldBusiness.getDescription())) {
            newBusiness.setDescription(oldBusiness.getDescription());
        }

        if (!StringUtils.isEmpty(oldBusiness.getCredit())) {
            newBusiness.setCredit(oldBusiness.getCredit());
        }

        if (!StringUtils.isEmpty(oldBusiness.getIsGroom())) {
            newBusiness.setIsGroom(oldBusiness.getIsGroom());
        }

        if (!StringUtils.isEmpty(oldBusiness.getNotice())) {
            newBusiness.setNotice(oldBusiness.getNotice());
        }

        if (oldBusiness.getAdmin() != null) {
            newBusiness.setAdmin(oldBusiness.getAdmin());
        }
        return newBusiness;
    }
}
